Skip to content

fix: guard against empty choices and message=None in LLM responses#39

Merged
yifanfeng97 merged 1 commit into
iMoonLab:mainfrom
qizwiz:fix/guard-unguarded-llm-choices
Jun 27, 2026
Merged

fix: guard against empty choices and message=None in LLM responses#39
yifanfeng97 merged 1 commit into
iMoonLab:mainfrom
qizwiz:fix/guard-unguarded-llm-choices

Conversation

@qizwiz

@qizwiz qizwiz commented May 17, 2026

Copy link
Copy Markdown
Contributor

Problem

OpenAI-compatible APIs can fail in two ways that crash the evaluation pipeline:

  1. Empty choices list (IndexError): response.choices is [] when the API returns no completions
  2. message=None (AttributeError): response.choices[0].message is None when content is filtered (e.g., Gemini returns HTTP 200 with PROHIBITED_CONTENT, message=None)

Affected files

  • evaluate/evaluate_by_scoring.py — 1 site
  • evaluate/evaluate_by_selection.py — 1 site
  • reproduce/Step_2_extract_question.py — 1 site

3 total sites guarded. 6 lines added, 0 deleted.

Fix pattern

if not response.choices or response.choices[0].message is None:
    raise ValueError("LLM returned empty or filtered response")

Verification

Detected by pact static analysis. Confirmed live: Gemini 2.5 Flash returns HTTP 200 with choices[0].message=None on prohibited content.

OpenAI-compatible APIs can return empty choices (IndexError) or
choices[0].message=None on filtered content (HTTP 200, AttributeError).

Adds guard before 3 bare choices[0].message.content accesses in
evaluate/ and reproduce/ scripts.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@yifanfeng97 yifanfeng97 merged commit 46ff126 into iMoonLab:main Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants